package com.yanqu.road.dao.impl.translations;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.translations.Translations;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/4/28 20:49
 */
public class TranslationsDaoImpl extends TempDataObjectDao<Translations> {

    @Override
    protected boolean updateData(Translations data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_translations SET time=?, useTimes=? WHERE srcContent=? and language=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getTime());
            call.setLong(i++, data.getUseTimes());
            call.setString(i++, data.getSrcContent());
            call.setString(i++, data.getLanguage());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected Translations parseData(ResultSet rs) throws SQLException {
        Translations data = new Translations();
        data.setSrcContent(rs.getString("srcContent"));
        data.setResultContent(rs.getString("resultContent"));
        data.setTime(rs.getLong("time"));
        data.setUseTimes(rs.getLong("useTimes"));
        data.setLanguage(rs.getString("language"));
        return data;
    }

    public Map<String, Map<String, Translations>> getChatLanguageMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<String, Translations>> map = new ConcurrentHashMap<>();
        String sql = "select * from t_u_translations";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                Translations data = parse(rs);
                if (!map.containsKey(data.getSrcContent())) {
                    map.put(data.getSrcContent(), new ConcurrentHashMap<>());
                }
                map.get(data.getSrcContent()).put(data.getLanguage(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    @Override
    protected boolean addData(Translations data) {
        PreparedStatement call = null;
        String sql = "REPLACE INTO t_u_translations(srcContent,resultContent,language,time,useTimes) VALUES(?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getSrcContent());
            call.setString(i++, data.getResultContent());
            call.setString(i++, data.getLanguage());
            call.setLong(i++, data.getTime());
            call.setLong(i++, data.getUseTimes());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }
}
